<?php

class SystemLog_IndexController extends Zend_Controller_Action
{
	protected $_object;

	public function init()
	{
		//tạo đối tượng chính của module
		$this->_object = new Admin_Model_SystemLog ();
		
		//lấy tiêu đề 
		$this->_helper->common->setCaptions ( array (), array ('system-log', 'action' ) );
	}

	public function historyAction()
	{
		$request = $this->getRequest ();
		$username = Zend_Registry::get('auth')->account_username;
		$this->_forward("index","index","system-log",array("username"=>$username));
	}
	
	//xem chi tiết 1 dòng	
	public function detailAction()
	{
		//lấy tham số
		$request = $this->_request;
		$id = $request->getParam ( 'id' );
		
		//lấy đối tượng cần cập nhật
		$this->_object->findBase ( $id );
		
		//tạo form
		$form = new SystemLog_Form_SystemLog_Detail ( $this );
		
		//đổ dữ liệu vào form 
		$form->populate ( $this->_object->toArray () );
		
		$form->system_log_time->setValue ( date ( 'H:i:s d.m.Y', $form->system_log_time->getValue () ) );
		
		//truyền dữ liệu ra view
		$this->view->form = $form;
	}

	//xem danh sách tất cả 
	public function indexAction()
	{
		//lấy tham số
		$request = $this->_request;
		$account_username = $request->getParam ( 'account_username', '' );
		$action_url = $request->getParam ( 'action_url', '' );
		$username = $request->getParam ( 'username', '' );
		
		//các cột cần lấy dữ liệu
		$select = " 
		SELECT
			`system_log_id`
			, `system_log_user`
			, `system_log_time`
			, `system_log_url`
			, `system_log_param`
			, `system_log_module`
			, `system_log_controller`
			, `system_log_action`
			, `action_note`
		FROM tbl_system_log
			LEFT JOIN tbl_action
				ON CONCAT(system_log_module,'/',system_log_controller,'/',system_log_action) = action_url 
		";
		
		//tạo form
		$form = new SystemLog_Form_SystemLog_Search ( $this );
		
		//Lay thong tin duong dan
		$action = new Admin_Model_Action ();
		$action_options = $action->fetchAllBase ( NULL, "action_url ASC" );
		$action_options = $action->toArray2 ( $action_options, 'action_url', '$row->action_url - $row->action_note' );
		$form->action_url->setMultiOptions ( array ('' => '- Chọn -' ) + $action_options );
		$form->action_url->setValue ( $action_url );
		
		//Dua vao select trong form
		if($username=="")
		{
			//lay thong tin tai khoan nguoi dung
			$account = new Admin_Model_Account ();
			$account_options = $account->fetchAllBase ( NULL, "account_username ASC" );
			$account_options = $account->toArray2 ( $account_options, 'account_username', 'account_username' );
			$form->account_username->setMultiOptions ( array ('' => '- Chọn -' ) + $account_options );					
			$form->account_username->setValue ( $account_username );			
		}
		else 
		{
			$form->removeElement('account_username');
		}
		
		//thiết lập điều kiện lấy dữ liệu
		$where = " WHERE 1 ";
		if ($account_username != '')
			$where .= " AND `system_log_user` = '$account_username' ";
		if ($action_url != '')
			$where .= " AND `action_url` = '$action_url' ";
		if($username!="")
			$where .= " AND `system_log_user` = '$username' ";
		
		//thiết lập thứ tự lấy dữ liệu
		$order = $this->_helper->common->getOrderString ( array ('system_log_time DESC' ) );
		
		//lấy dữ liệu
		$query = $select . $where . $order;
		$data = $this->_object->queryBase ( $query );
		
		//phân trang
		$this->_helper->common->paginate ( $data );
		
		//truyền dữ liệu ra view
		$this->view->form = $form;
		$this->view->account_username = $account_username;
		$this->view->action_url = $action_url;
	}
}